package com.lance.answer.gui.answer.gk.answer;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.lance.answer.gui.answer.AnswerObject;
import com.lance.answer.gui.answer.gk.DealInfoUtil;
import com.lance.answer.gui.answer.gk.answer.task.UploadFileTask;
import com.lance.answer.gui.answer.gk.answer.task.UploadFileTasktmmmmmp;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.LinkedBlockingDeque;

/**
 * 解决大作业字数》1000
 */
@Slf4j
public class UploadFiletmpppp {

    /**
     * 进度表：
     * 6-18 19:55开始测试 公共行政学 行政管理专科
     *
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {

      //  Thread.sleep(1000*60*60*2);

        int sync = 8;

        Map<String,List<String>> all = Maps.newHashMap();

        all.put("习近平新时代中国特色社会主义思想",Lists.newArrayList("http://xinjiang.ouchn.cn/mod/assign/view.php?id=504159")); //1111
        all.put("毛泽东思想和中国特色社会主义理论体系概论",Lists.newArrayList("http://xinjiang.ouchn.cn/mod/assign/view.php?id=504446",
                "http://xinjiang.ouchn.cn/mod/assign/view.php?id=503821"));
        all.put("思想道德修养与法律基础",Lists.newArrayList("http://xinjiang.ouchn.cn/mod/assign/view.php?id=504309",
                "http://xinjiang.ouchn.cn/mod/assign/view.php?id=503556"));
        all.put("马克思主义基本原理概论",Lists.newArrayList("http://xinjiang.ouchn.cn/mod/assign/view.php?id=503680"));


        all.put("中国近现代史纲要",Lists.newArrayList("http://xinjiang.ouchn.cn/mod/assign/view.php?id=503975",
                "http://xinjiang.ouchn.cn/mod/assign/view.php?id=503975")); //todo


        all.put("思想道德与法治",Lists.newArrayList("http://xinjiang.ouchn.cn/mod/assign/view.php?id=503264"));

       //  有两套！！！！！



    //    List<String> subjectList = Lists.newArrayList();
        /**
         * http://xinjiang.ouchn.cn/mod/assign/view.php?id=242355 人数多
         * http://xinjiang.ouchn.cn/mod/assign/view.php?id=242482
         * 大作业yo两种链接
         */
      //  subjectList.add("思想道德修养与法律基础");

        LinkedBlockingDeque<AnswerDTO> answerQueue = new LinkedBlockingDeque<>();

        List<String> teachers = Lists.newArrayList();
        teachers.add("刘庆");
        teachers.add("李明");
        teachers.add("刘");
        teachers.add("李俊杰");
        teachers.add("艾");
        teachers.add("宋柳萌");
        teachers.add("朱晓玉");
        teachers.add("零散");



        for(String teacher:teachers){
            // 获取所有的用户名和密码
            InputStream userInputStream = new FileInputStream("libs//teachers//"+teacher+".user");
            List<String> userString = IOUtils.readLines(userInputStream, "utf-8");
            Map<String, String> usernameAndPwd = Maps.newHashMap();
            for (String user : userString) {
                String[] strs = user.split(",");
                String userCode = strs[0];
                String pwd = "";
                if(strs[1].length()<18){
                    pwd = strs[1];
                }else {
                    pwd = strs[1].substring(6, 14);
                    pwd = "Ouchn" + pwd;
                }
                usernameAndPwd.put(userCode, pwd);
            }

            for(String subjectName:all.keySet()){
                //  LinkedList<AnswerRequestDTO> requestUrl = Lists.newLinkedList();
                LinkedList<String> requestUrl = Lists.newLinkedList(all.get(subjectName));

                log.info("subjectName:{}",subjectName);
                InputStream answerInputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(subjectName + "/" + subjectName + "-answer.json");
                String answerStr = IOUtils.toString(answerInputStream,"UTF-8");
                List<AnswerObject> list = JSONObject.parseArray(answerStr, AnswerObject.class);
                Map<String, AnswerObject> answers = new HashMap<>();
                for (AnswerObject answerObject : list) {
                    answers.put(answerObject.getQuestionCode(), answerObject);
                }

                // 根据课程名称拿到 所有人 这里的用户只有用户名，没有密码
                List<String> userNames = DealInfoUtil.countUserCourseInfo("libs//userCourseInfo//"+teacher).get(subjectName);
                if(userNames == null || userNames.size()<1){
                    continue;
                }
                for(String username:userNames){
//                if(!Util.getVip().contains(username)){
//                    continue;
//                }
                    String pwd = usernameAndPwd.get(username);
                    AnswerDTO answerDTO = new AnswerDTO();
                    answerDTO.setSubjectName(subjectName);
                    answerDTO.setUsername(username);
                    answerDTO.setPwd(pwd);
                    answerDTO.setAnswers(answers);
                    answerDTO.setCommit(true);
                    answerDTO.setTeacher(teacher);
                    answerDTO.setPush(false);
                    answerDTO.setUploadUrl(requestUrl);
//                    if(!subjectName.equals("中国近现代史纲要")){
//                        continue;
//                    }
                    answerQueue.add(answerDTO);
                }

            }
        }




        for (int i = 1; i <= sync; i++) {
            Thread answerThread = new UploadFileTasktmmmmmp(null,answerQueue);
            //log.info("加载课程ID：{} 名称：{} ", openCourseId,subjectName);
            answerThread.setName( "答题线程--" + i);
            answerThread.start();
        }


    }






}
